package com.coder.shop.admin.controller.manage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coder.common.base.BaseController;
import com.coder.shop.common.constant.ShopResult;
import com.coder.shop.common.constant.ShopResultConstant;
import com.coder.shop.dao.model.ShopPromotionGoodsRel;
import com.coder.shop.dao.model.ShopPromotionGoodsRelExample;
import com.coder.shop.rpc.api.ShopPromotionGoodsRelService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 促销关联controller
 * @Author liuzhimin
 * @Date Created by liuzhimin on 2018/4/24.
 * @Description
 */
@Controller
@RequestMapping("/manage/promotionGoodsRel")
@Api(value = "促销关联控制器", description = "促销关联管理")
public class ShopPromotionGoodsRelController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ShopPromotionGoodsRelController.class);

    @Autowired
	private ShopPromotionGoodsRelService shopPromotionGoodsRelService;
	
	@ApiOperation(value = "促销关联首页")
	@RequiresPermissions("shop:promotionGoodsRel:read")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {
		return "/manage/promotionGoodsRel/index.jsp";
	}

	@ApiOperation(value = "促销关联列表")
	@RequiresPermissions("shop:promotionGoodsRel:read")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Object list(
			@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
			@RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
			@RequestParam(required = false, value = "sort") String sort,
			@RequestParam(required = false, value = "order") String order) {
		ShopPromotionGoodsRelExample shopPromotionGoodsRelExample = new ShopPromotionGoodsRelExample();
		if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
			shopPromotionGoodsRelExample.setOrderByClause(sort + " " + order);
		}
		List<ShopPromotionGoodsRel> rows = shopPromotionGoodsRelService.selectByExampleForOffsetPage(shopPromotionGoodsRelExample, offset, limit);
		long total = shopPromotionGoodsRelService.countByExample(shopPromotionGoodsRelExample);
		Map<String, Object> result = new HashMap<>(2);
		result.put("rows", rows);
		result.put("total", total);
		return result;
	}
	
	@ApiOperation(value = "促销关联新增")
	@RequiresPermissions("shop:promotionGoodsRel:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(ModelMap modelMap) {
		return "/manage/promotionGoodsRel/create.jsp";
	}

	@ApiOperation(value = "促销关联新增")
	@RequiresPermissions("shop:promotionGoodsRel:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public Object create(ShopPromotionGoodsRel shopPromotionGoodsRel) {
		int count = shopPromotionGoodsRelService.insertSelective(shopPromotionGoodsRel);
		return new ShopResult(ShopResultConstant.SUCCESS, count);
	}
	
	}